package com.note.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.note.pojo.Note;
import com.note.vo.TNote;
import com.note.pojo.User;
import com.note.service.CommService;
import com.note.service.NotLikeService;
import com.note.service.NoteService;
import com.note.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;


@Controller
public class IndexController {
    @Autowired
    private NoteService noteService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommService commService;
    @Autowired
    private NotLikeService notLikeService;

    @RequestMapping("/main")
    public String main(){
        return "mains";
    }

    @RequestMapping("/index")
    public String index(@RequestParam(value = "pn",defaultValue = "1") Integer pn, HttpSession session, Model model){
        PageHelper.startPage(pn,6);
        User user = (User) session.getAttribute("user");
        Integer userid= user.getId();
        List<Note> notes = noteService.listNoteByUid(userid);
        PageInfo page = new PageInfo(notes, 5);
        model.addAttribute("pageInfo",page);
        return "index1";
    }
    //跳转到仅修改页面
    @RequestMapping("/onlyalter")
    public String onlyalter(@RequestParam(value = "pn",defaultValue = "1") Integer pn,HttpSession session, Model model){
        PageHelper.startPage(pn,6);
        User user = (User) session.getAttribute("user");
        Integer userid= user.getId();
        List<Note> notes = noteService.listNoteByUid(userid);
        PageInfo pageInfo=new PageInfo(notes,5);
        model.addAttribute("pageInfo",pageInfo);
        return "onlyalter";
    }

    //跳转到个人笔记仅修改页面
    @RequestMapping("/onlyalter0")
    public String onlyalter0(@RequestParam(value = "pn",defaultValue = "1") Integer pn,HttpSession session, Model model){
        PageHelper.startPage(pn,6);
        User user = (User) session.getAttribute("user");
        Integer userid= user.getId();
        List<Note> notes = noteService.listByUidTy(userid,0);
        PageInfo pageInfo=new PageInfo(notes,5);
        model.addAttribute("pageInfo",pageInfo);
        return "onlyalter0";
    }

    //跳转到分享笔记仅修改页面
    @RequestMapping("/onlyalter1")
    public String onlyalter1(@RequestParam(value = "pn",defaultValue = "1") Integer pn,HttpSession session, Model model){
        PageHelper.startPage(pn,6);
        User user = (User) session.getAttribute("user");
        Integer userid= user.getId();
        List<Note> notes = noteService.listByUidTy(userid,1);
        PageInfo pageInfo=new PageInfo(notes,5);
        model.addAttribute("pageInfo",pageInfo);
        return "onlyalter1";
    }
    //跳转到笔记分享页
    @RequestMapping("/shareindex")
    public String shareindex(@RequestParam(value = "pn",defaultValue = "1") Integer pn,HttpSession session, Model model){
        int size =(int) Math.ceil((double) noteService.getNotesByType(1).size()/7);
        PageHelper.startPage(pn,7);
        User user = (User) session.getAttribute("user");
        List<Note> noteList = noteService.getNotesByType(1);

        List<TNote> notes = noteList.stream().map(note1 -> {
            TNote note = new TNote();
            BeanUtils.copyProperties(note1, note);
            note.setId(note1.getId());
            note.setContent(note1.getContent());
            note.setTitle(note1.getTitle());
            note.setType(note1.getType());
            note.setUserid(note1.getUserid());
            note.setCreatetime(note1.getCreatetime());
            note.setAvatar(userService.findAvatarById(note1.getUserid()));
            note.setLoginname(userService.findNameById(note1.getUserid()));
            note.setTalks(commService.getComsByNoteId(note1.getId()).size());
            note.setLikes(notLikeService.sumNlikes(note1.getId()));
            return note;
        }).collect(Collectors.toList());

        PageInfo pageInfo=new PageInfo(notes,5);
        System.out.println(pageInfo.getList());
        model.addAttribute("user",user);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("size",size);
        return "shareindex";
    }

    //跳转到笔记添加页面
    @RequestMapping("/noteadd")
    public String noteadd(){
        return "noteadd";
    }

    //跳转到忘记密码页面
    @RequestMapping("/forgetpwd")
    public String forgetpwd(){
        return "forgetpwd";
    }
}
